package com.tr.syscms.controller;


import com.tr.common.util.Result;
import com.tr.common.util.ResultStatus;
import com.tr.syscms.entity.User;
import com.tr.syscms.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author xiijava
 * @since 2022-06-07
 */
//@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

//    @PostMapping("queryAll")
//    public Result queryAllUser(@RequestBody Map map){
//        return new Result(ResultStatus.SUCCESS.getReturncode(),ResultStatus.SUCCESS.getReturnMessage(),
//                this.userService.queryAll(map));
//    }
    @GetMapping("/queryByUserName")
    public User queryByUserName(@RequestParam("userName") String userName){
        return userService.queryUserByUserName(userName);
    };


    /**
     * 用户登录
     *
     * @param user
     * @return
     */
    @GetMapping("login")
    public Result login(User user) {
        return userService.login(user);
    }

    /**
     * 用户退出
     *
     * @return
     */
    @GetMapping("logout")
    public Result logout() {
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        session.removeAttribute("userInfo");
        return new Result(ResultStatus.SUCCESS.getReturncode(),
                ResultStatus.SUCCESS.getReturnMessage(), null
        );
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    //@RequiresPermissions("user:select")
    public Result selectOne(Integer id) {
        return new Result(ResultStatus.SUCCESS.getReturncode(),
                ResultStatus.SUCCESS.getReturnMessage(),
                this.userService.queryById(id));
    }

    /**
     * 分页带参数查询
     *
     * @param map 参数
     * @return
     */
    @PostMapping("queryAll")
    public Result queryAll(@RequestBody Map map) {
        return new Result(ResultStatus.SUCCESS.getReturncode(),
                ResultStatus.SUCCESS.getReturnMessage(),
                this.userService.queryAll(map));
    }

    /**
     * 添加
     *
     * @param user
     * @return
     */
    @PutMapping("add")
    public Result add(@RequestBody User user) {
        return new Result(ResultStatus.SUCCESS.getReturncode(),
                ResultStatus.SUCCESS.getReturnMessage(),
                this.userService.insert(user));
    }

    /**
     * 人员头像上传
     *
     * @param file
     * @return
     */
    @RequestMapping("uploadUserHeadPic")
    public Result uploadUserHeadPic(@RequestParam MultipartFile file) {
        return new Result(ResultStatus.SUCCESS.getReturncode(),
                ResultStatus.SUCCESS.getReturnMessage(),
//                https://aiops03bucketa.oss-cn-beijing.aliyuncs.com/files/user/images/xxx.png
//                null);
                this.userService.uploadUserHeadPic(file));
    }

    /**
     * 更新
     *
     * @param user
     * @return
     */
    @PutMapping("update")
    public Result update(@RequestBody User user) {
        return new Result(ResultStatus.SUCCESS.getReturncode(),
                ResultStatus.SUCCESS.getReturnMessage(),
                this.userService.update(user));
    }

    /**
     * 根据编号删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("deleteById")
    public Result deleteById(Integer id) {
        return new Result(ResultStatus.SUCCESS.getReturncode(),
                ResultStatus.SUCCESS.getReturnMessage(),
                this.userService.deleteById(id));
    }


}

